package com.itheima.admin.listener;

import com.itheima.admin.service.WemediaNewsAutoScanService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class WemediaNewsAutoScanListener {
    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_AUTO_SCAN_TOPIC)
    public void autoScanWemediaNews(ConsumerRecord<String,String> record){
        //创建扫描后唯一标识--创建uuid
        String uuid = UUID.randomUUID().toString();

        log.info("自动审核开始：uuid={},id={},topic={},partition={}",
                uuid,record.value(),record.topic(),record.partition());

        try {
            wemediaNewsAutoScanService.autoScanWemediaNewsById(Integer.valueOf(record.value()),uuid);
        } catch (NumberFormatException e) {
            log.info("【出错】文章自动审核：uuid={},id={},topic={},partition={}",
                    uuid,record.value(),record.topic(),record.partition());
        }
        log.info("自动审核结束：uuid={},id={},topic={},partition={}",
                uuid,record.value(),record.topic(),record.partition());
    }
}
